package com.cyj.dream.core.constant.users;

import cn.hutool.core.util.StrUtil;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 基础信息DTO查询帮助类全局使用即可https://blog.csdn.net/qq_37164847/article/details/87859674
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.core.constant.users
 * @Author: ChenYongJia
 * @CreateTime: 2021-04-17 09:18
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class BasicInfoSelect {

    public static BasicInfoSelect basicInfoSelect;

    /**
     * @Description: 获取每个请求的 token
     * @return: String
     * @Author: ChenYongJia
     * @CreateTime: 2020/12/14 16:25
     */
    public static String getHeaderToken() {
        // 获取当前请求 request 上下文信息
        HttpServletRequest request = RequestContext.getCurrentRequest();
        log.info("进入获取当前登录用户上下文方法，参数 request :{}", request.toString());

        // 获取请求头中JWT的Token
        String tokenHeader = request.getHeader(Constant.tokenHeader);
        log.info("----------请求头拿到的 token 数据信息---------->{}", tokenHeader);

        if (StrUtil.isEmpty(tokenHeader)) {
            log.error("出现错误：参数缺失根据token获取用户信息失败！参数 token 为空，返回错误提示用户未登录（未获取到 token 值）");
            throw new BizException(ResultCode.USER_NOT_LOGGED_IN);
        }
        return tokenHeader;
    }

    /**
     * 关于下面注解的解释：https://blog.csdn.net/qq360694660/article/details/82877222
     * 初始化改类
     *
     * @return void
     * @PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序：
     * Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
     * @date 2021/6/1 17:28
     * @author ChenYongJia
     * @version 1.0
     */
    @PostConstruct
    public void init() {
        basicInfoSelect = this;
    }

}
